<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
<!--    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">-->
    <link rel="stylesheet"  href="../css/index.css">
    <script src="../js/axios.min.js" type="text/javascript"></script>
    <script src="../js/vue.js" type="text/javascript"></script>
    <script src="../js/index.js" type="text/javascript"></script>
<!--    <script src="https://unpkg.com/element-ui/lib/index.js"></script>-->
    <title>用户注册</title>
</head>
<body>
<div id="app">
    <el-row style="margin-top: 150px;">
        <el-col :span="8" :offset="8">
            <el-form :model="venueRegisterForm" status-icon :rules="rules" ref="venueRegisterForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="场馆账号" prop="accountName">
                    <el-input v-model="venueRegisterForm.accountName" placeholder="请输入注册账号" @change="checkEmail()"></el-input>
                </el-form-item>



                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="venueRegisterForm.email" placeholder="请输入注册邮箱" @change="checkEmail()"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="venueRegisterForm.password" autocomplete="off" placeholder="密码"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPassword">
                    <el-input type="password" v-model="venueRegisterForm.checkPassword" autocomplete="off" placeholder="再次输入密码"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('venueRegisterForm')">立即注册</el-button>
                    <el-button @click="resetForm('venueRegisterForm')">重置</el-button>
                    <el-button @click="toLogin">去登录</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</div>
</body>
<script>
    new Vue({
        el: "#app",
        data() {
            let checkEmail = (rule, value, callback) => {
                const data = /^([a-zA-Z0-9]+[-_\.]?)+@[a-zA-Z0-9]+\.[a-z]+$/;
                if (!value) {
                    return callback(new Error('邮箱不能为空'));
                }else if (!data.test(value)){
                    return callback(new Error('请输入正确格式的邮箱'));
                }
                callback()

            };
            let validatePassword = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.venueRegisterForm.checkPassword !== '') {
                        this.$refs.venueRegisterForm.validateField('checkPassword');
                    }
                    callback();
                }
            };
            let validatePassword2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.venueRegisterForm.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                submit:true,
                venueRegisterForm: {
                    accountName:'',
                    email: '',
                    password: '',
                    checkPassword: ''
                },
                rules: {
                    password: [
                        { validator: validatePassword, trigger: 'blur' }
                    ],
                    checkPassword: [
                        { validator: validatePassword2, trigger: 'blur' }
                    ],
                    email: [
                        { validator: checkEmail, trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            toLogin(){
                window.location.href='venueLogin.html'
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    console.log(this.submit)
                    if (valid && this.submit) {
                        axios.post("/venue/venueRegister", this.venueRegisterForm).then(resp=>{
                            if(resp.status===resp.data.code){
                                this.$message({
                                    type: 'success',
                                    message: '注册成功 请留意查收激活邮件'
                                });
                                setTimeout(() => {
                                    location.href = 'venueLogin.html'
                                }, 3000);
                            }else{
                                alert("注册失败 请检查原因！")
                            }
                        })
                    } else {
                        this.$message({
                            message: '请修改信息后再提交',
                            type: 'warning'
                        });
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            checkEmail(){
                axios.get("/venue/testEmail?email="+this.venueRegisterForm.email).then(resp=>{
                    if(resp.status===resp.data.code){
                        this.submit = false
                        this.$message({
                            message: '该邮箱已被注册',
                            type: 'warning'
                        });
                    }else{
                        this.submit = true
                    }
                })
            }
        }
    })
</script>
</html>